package com.lemon.core.validator.configuration;

import com.lemon.core.validator.annotation.EnableFastFailValidator;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

import javax.validation.Validation;
import javax.validation.Validator;

/**
 * 参数校验配置
 * 直接使用{@link org.springframework.context.annotation.Configuration}将开启全局快速失败
 * 配合使用{@link EnableFastFailValidator}灵活配置
 *
 * @author pan
 * @since 2019-01-27
 */
public class ValidatorConfiguration {

    /**
     * 校验@RequestBody时，采用快速失败模式
     */
    @Bean
    public Validator validator() {
        // false：普通模式(会校验完所有的属性，然后返回所有的验证失败信息)
        // true：快速失败返回模式(只要有一个验证失败，则返回)
        return Validation.byProvider(HibernateValidator.class).configure()
                .failFast(true).buildValidatorFactory().getValidator();
    }

    /**
     * 校验@RequestParam时，采用快速失败模式
     */
    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor(Validator validator) {
        MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor();
        // 设置validator
        postProcessor.setValidator(validator);
        return postProcessor;
    }
}
